<template>
  <div class="login-container">
    <div class="login-box">
      <h2>登录小米有品</h2>
      <form @submit.prevent="handleLogin">
        <div class="form-group">
          <input 
            type="text" 
            v-model="username" 
            placeholder="用户名/邮箱"
            required
          >
        </div>
        <div class="form-group">
          <input 
            type="password" 
            v-model="password" 
            placeholder="密码"
            required
          >
        </div>
        <div class="error-message" v-if="error">{{ error }}</div>
        <button type="submit" class="login-btn">登录</button>
      </form>
      <div class="links">
        <router-link to="/register">立即注册</router-link>
        <a href="#">忘记密码？</a>
      </div>
    </div>
  </div>
</template>

<script>
import userData from '@/assets/users.json'

export default {
  data() {
    return {
      username: '',
      password: '',
      error: ''
    }
  },
  methods: {
    handleLogin() {
      const user = userData.users.find(
        u => u.username === this.username && u.password === this.password
      )
      
      if (user) {
        // 登录成功，存储用户信息
        localStorage.setItem('currentUser', JSON.stringify(user))
        this.$router.push('/')
      } else {
        this.error = '用户名或密码错误'
      }
    }
  }
}
</script>

<style scoped>
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: #f5f5f5;
}

.login-box {
  background: white;
  padding: 30px;
  border-radius: 8px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.1);
  width: 100%;
  max-width: 400px;
}

h2 {
  text-align: center;
  margin-bottom: 30px;
  color: #333;
}

.form-group {
  margin-bottom: 20px;
}

input {
  width: 100%;
  padding: 12px;
  border: 1px solid #ddd;
  border-radius: 4px;
  font-size: 16px;
  outline: none;
}

input:focus {
  border-color: #ff6700;
}

.login-btn {
  width: 100%;
  padding: 12px;
  background: #ff6700;
  color: white;
  border: none;
  border-radius: 4px;
  font-size: 16px;
  cursor: pointer;
  transition: background 0.3s;
}

.login-btn:hover {
  background: #f25600;
}

.error-message {
  color: #ff4444;
  margin-bottom: 15px;
  font-size: 14px;
}

.links {
  display: flex;
  justify-content: space-between;
  margin-top: 20px;
  font-size: 14px;
}

.links a {
  color: #666;
  text-decoration: none;
}

.links a:hover {
  color: #ff6700;
}
</style> 